package api

import (
	"openapi.itspire.cn/common/dict"
	"openapi.itspire.cn/common/global"
	"openapi.itspire.cn/database"
	"openapi.itspire.cn/model"
	"openapi.itspire.cn/utils"
	"github.com/gin-gonic/gin"
)

func LabelList(c *gin.Context) {
	var label model.ArticleLabel
	var category model.Category

	list := make([]model.ArticleLabel, 0)
	query := database.Ins.
		Table(label.GetTableName() + " a").
		Select("a.label_id,count(a.label_id) `count`,b.category_name label_name").
		Joins("left join " + category.GetTableName() + " b on a.label_id = b.id").
		Where("b.delete_time = ? AND b.status = ?", dict.DeleteNot, dict.StatusOn).
		Group("a.label_id").
		Order("`count` desc").
		Limit(global.LabelMax)
	if err := query.Scan(&list).Error; err != nil {
		panic(err)
	}

	utils.Success(c, list)
}
